package cn.iocoder.yudao.module.product.job;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSkuSaveReqVO;
import cn.iocoder.yudao.module.product.controller.admin.spu.vo.ProductSpuSaveReqVO;
import cn.iocoder.yudao.module.product.dal.dataobject.resourceshare.ResourceShareDO;
import cn.iocoder.yudao.module.product.dal.dataobject.sku.ProductSkuDO;
import cn.iocoder.yudao.module.product.dal.dataobject.spu.ProductSpuDO;
import cn.iocoder.yudao.module.product.dal.mysql.resourceshare.ResourceShareMapper;
import cn.iocoder.yudao.module.product.dal.mysql.sku.ProductSkuMapper;
import cn.iocoder.yudao.module.product.dal.mysql.spu.ProductSpuMapper;
import cn.iocoder.yudao.module.product.enums.spu.ProductSpuStatusEnum;
import cn.iocoder.yudao.module.product.service.brand.ProductBrandService;
import cn.iocoder.yudao.module.product.service.sku.ProductSkuService;
import cn.iocoder.yudao.module.promotion.dal.dataobject.diy.DiyPageDO;
import cn.iocoder.yudao.module.promotion.dal.mysql.diy.DiyPageMapper;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getMinValue;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.getSumValue;

/**
 * 定时更新首页数据
 */
@Component
@Slf4j
public class IndexSpuJob implements JobHandler {

    @Resource
    @Lazy // 循环依赖，避免报错
    private DiyPageMapper diyPageMapper;

    @Resource
    private ProductSpuMapper productSpuMapper;

    @Override
    @TenantJob // 标记多租户
    public String execute(String param) {
        log.info("开始进行定时更新首页数据");
        List<ProductSpuDO> productSpuDOS = productSpuMapper.selectList();

        if (ObjectUtil.isEmpty(productSpuDOS)){
            return "数据为空,不进行更新!";
        }
        List<Long> longList = productSpuDOS.stream()
                .sorted(Comparator.comparing(ProductSpuDO::getCreateTime).reversed())
                .limit(Long.parseLong(param))
                .map(ProductSpuDO::getId)
                .collect(Collectors.toList());
        DiyPageDO diyPageDO = diyPageMapper.selectById(10);
        if (ObjectUtil.isNotEmpty(diyPageDO)) {
            String property = diyPageDO.getProperty();
            JSONObject jsonObject = JSONObject.parseObject(property);
            List<Long> spuIds = new ArrayList<>();
            spuIds.addAll(longList);
            jsonObject.getJSONArray("components")
                    .getJSONObject(4)
                    .getJSONObject("property")
                    .put("spuIds", spuIds);
            String updatedJsonString = jsonObject.toString();
            diyPageDO.setProperty(updatedJsonString);
            diyPageMapper.updateById(diyPageDO);

        }
        return "success";
    }
}
